module namelist_mod

  use constants_mod

  implicit none

  integer :: t_final
  integer :: nx
  integer :: ncell
  real(r8) :: x_min, x_max
  real(r8) :: CFL, dx, u
  integer :: nhalo
  character(256) :: case_name, test_case, Riemann_solver
  real(r8), allocatable :: x(:)
  integer :: output_interval
  character(256) :: time_scheme, space_scheme, slope_limiter, flux_limiter

  namelist /params/ &
    t_final, &
    nx, &
    ncell, &
    cfl, &
    Riemann_solver, &
    output_interval, &
    test_case, &
    case_name, &
    space_scheme, &
    slope_limiter, &
    flux_limiter, &
    time_scheme

contains 

  subroutine parse_namelist(file_path)
		!===============================================
		!  |---o---|---o---|--o---|---o---|---o---|
		! -1                                      1
		!      1       2       3       4       5   ! nx=5
		!     x(1)    x(2)    x(3)    x(4)    x(5)
		! e.g., dx = 2 / nx = 2 / 5 = 0.4
		!===============================================
    character(len=*), intent(in) :: file_path
    integer :: i 
    x_min = -1.0_r8
    x_max = 1.0_r8

    open(10, file=file_path, status='old')
    read(10, nml=params)
    close(10)

    select case (trim(space_scheme))
    case ('MCV')
      nx = 2 * ncell + 1
      dx = (x_max - x_min) / ncell
      allocate(x(nx))
      do i = 1, nx
        x(i) = x_min + (i-1) * dx * 0.5_r8
      end do
    case default
      nx = nx
      dx = (x_max - x_min) / nx
      allocate(x(nx))
      do i = 1, nx
        x(i) = x_min + (i - 0.5_r8) * dx
      end do
    end select

    if (trim(space_scheme) == 'weno5') then
      nhalo = 3
    else
      nhalo = 2 !  MCV 2, WENO 3
    end if
    
    u = 1.0_r8

  end subroutine parse_namelist

end module namelist_mod